package org.ilead.iserve.service.manager;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.ilead.iserve.constants.LopConstants;
import org.ilead.iserve.constants.SessionKeyConstant;
import org.ilead.iserve.model.HomePage;
import org.ilead.iserve.model.User;
import org.ilead.iserve.service.IHomePageService;
import org.ilead.iserve.service.IUserService;

import com.opensymphony.xwork2.ActionContext;

public class UserManager {
	private static UserManager instance = new UserManager();
	private IUserService userService;
	/**
	 * 缓存所有审核通过的用户
	 */
	private List<User> cacheUserList;
	
	/**
	 * 缓存分页时当前的用户显示列表，用以方便修改
	 */
	private List<User> userList;
	
	private UserManager(){
	}
	
	public static UserManager getInstance(){
		return instance;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public Boolean hasNameRegister(String loginName){
		return userService.hasNameRegister(loginName);
	}
	
	public Boolean hasEmailRegister(String email){
		return userService.hasEmailRegister(email);
	}
	
	public User login(String username, String password){
		return userService.login(username, password);
	}
	
	/**
	 * 更新Session和数据库中User
	 * @param request
	 * @param user
	 * @throws Exception
	 */
	public void updateSessionAndUser(HttpServletRequest request,User user) throws Exception{
		HttpSession session = request.getSession();
		session.setAttribute(SessionKeyConstant.LOGIN_USER, user);
		HomePage page = HomePageManager.getInstance().getHomePageByUserId(user.getId());
		if(page != null){
			user.setHomePageUrl(page.getUrl());
			user.setSignature(page.getSignature());
		}
		user.setLastLoginTime(new Date());
		session.setAttribute(SessionKeyConstant.MESSAGE_COUNT, MessageManager.getInstance().getMessageLength(user.getId()));
		session.setAttribute(SessionKeyConstant.AVATAR_URL, getAvatarUrl(request, user));
		userService.updateUser(user);
	}
	
	public String getAvatarUrl(HttpServletRequest request, User user){
		String path = request.getContextPath();
		String image = user.getImage();
		if(image == null || "".equals(image))
			image = "defaultAvatar.png";
		
		return path + LopConstants.UPLOAD_AVATAR_PATH + File.separatorChar + image;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}
	
	/**
	 * 获取所有普通会员以上的用户
	 * @return
	 */
	public List<User> getAllRegisterUser(){
		if(cacheUserList == null){
			cacheUserList = userService.findByPermission(2);
		}
		return cacheUserList;
	}
	
	/**
	 * 更新缓存的用户列表
	 * 
	 */
	public void updateCacheUserList(){
		cacheUserList = userService.findByPermission(2);
	}
	
	public User findById(int id){
		for (User user : getAllRegisterUser()){
			if(user.getId().intValue() == id){
				return user;
			}
		}
		return userService.findById(id);
	}
	
	public User getLoginUser(){
		return (User)ActionContext.getContext().getSession().get(SessionKeyConstant.LOGIN_USER);
	}
	
}
